//
//  HZGoodsDetailViewController.m
//  app
//
//  Created by 刘华舟 on 15/5/18.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZGoodsDetailViewController.h"
#import "HZProuctCommentsViewController.h"
#import "HZProductTextImageDetailViewController.h"
#import "MJPhotoBrowser.h"
#import "HZMallGoodsListViewController.h"
#import "HZGoodsSalesViewController.h"
#import "HZNormalWebViewController.h"
#import "HDChatOBO.h"

//model
#import "HZGoodsDetailVCData.h"
#import "HZProductCommentsVCData.h"
#import "HZRemoteResult.h"
//view
#import "HZTableView.h"

#import "HZGoodsDetailPicHeadView.h"
#import "HZGoodsDetailHeadView.h"
#import "HZProductTextImageDetailFootView.h"
#import "HZGoodsDetailTotalCmtView.h"

#import "HZGoodsCommentCell.h"
#import "HZOrderGoodsEmptyView.h"
#import "HZGoodsDetailToolPanel.h"
#import "HZGoodsDetailNaviBar.h"
#import "HZGoodsLoadMoreView.h"
#import "HZProductConfirmView.h"
#import "HZProductDetailSecView.h"
#import "HZProductTextImageDetailCell.h"
#import "HZGoodsParametersCell.h"

//tool
#import "HZMallHttpManager.h"
#import "HDGoodsCollectManager.h"
#import "HZMallSqlDataManager.h"
#import "NSDictionary+SafetyGet.h"
#import "UIImageView+URL.h"

#import "NSString+Conveniently.h"

//category
#import "MBProgressHUD+Conveniently.h"


static const NSInteger kGoodsToolPanelHeight = kGoodsDetailToolPanelMainViewHeight;

@interface HZGoodsDetailViewController()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, UIAlertViewDelegate>


@property (strong, nonatomic) HZTableView *tableView;


@property (strong, nonatomic) HZGoodsDetailPicHeadView *picHeadView;
@property (strong, nonatomic) HZGoodsDetailHeadView *detailSecHeaderView;
@property (strong, nonatomic) HZProductTextImageDetailFootView* pslcSecHeaderView;
@property (strong, nonatomic) HZGoodsDetailTotalCmtView* totalCmtHeaderView;


@property (strong, nonatomic) HZOrderGoodsEmptyView *emptyView;


@property (strong, nonatomic) HZGoodsDetailToolPanel *toolPanel;
@property (strong, nonatomic) HZGoodsDetailNaviBar *naviBar;


@property (strong, nonatomic) UIButton *backBtn;

@property (strong, nonatomic) HZGoodsDetailVCData *data;

@property (strong, nonatomic) HZRemoteResult *remoteResult;

@property (strong, nonatomic) HZRemoteResult *addCartResult;
@property (strong, nonatomic) HZRemoteResult *buyNowResult;
@property (strong, nonatomic) HZRemoteResult *favoriteResult;



@end

@implementation HZGoodsDetailViewController


- (NSString *)shopID{
    if (_shopID == nil) {
        _shopID = @"";
    }
    return _shopID;
}

- (NSString *)productID{
    if (_productID == nil) {
        _productID = @"";
    }
    return _productID;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HZ_WHITE_COLOR;
    
    self.title = @"商品详情";
    
    //关闭自动适应UIScrollView
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.hidden = NO;
    //    Bar的模糊效果，默认为YES
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = nil;
    
    self.picHeadView = [[HZGoodsDetailPicHeadView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP], [self.ubo fw:UB_WIDTH uh:0])];
    self.picHeadView.picsTargetCaller = [UBTargetCaller of:self action:@selector(seeProductsBigPictures:)];
    
    self.detailSecHeaderView = [[HZGoodsDetailHeadView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP], [self.ubo fw:UB_WIDTH uh:20])];
    self.detailSecHeaderView.targetCallerService = [UBTargetCaller of:self action:@selector(serviceBtnOnClick)];
    
    self.pslcSecHeaderView = [[HZProductTextImageDetailFootView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP], [self.ubo fw:UB_WIDTH uh:20])];
    self.pslcSecHeaderView.targetCallerSeeDeliveryInfo = [UBTargetCaller of:self action:@selector(seeViewNotice)];
    
    self.totalCmtHeaderView = [[HZGoodsDetailTotalCmtView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP], [self.ubo fw:UB_WIDTH uh:20])];
    self.totalCmtHeaderView.seeAllTargetCaller = [UBTargetCaller of:self action:@selector(seeAllComments)];
    
    
    
    //添加tableView
    self.tableView = [[HZTableView alloc] initWithFrame:CGRectOf(CGPointMake(0, 0),
                                                                 [self.view.ubo fw:UB_WIDTH fh:UB_HEIGHT]) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = HZ_MALL_M_LIGHT_GRAY_BACKGROUND_COLOR;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(self.navigationAndStatusBarHeight, 0, UF(kGoodsToolPanelHeight), 0);
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(self.navigationAndStatusBarHeight, 0, UF(kGoodsToolPanelHeight), 0);
    self.tableView.sectionHeaderHeight = 0.1f;
    self.tableView.sectionFooterHeight = 0.1f;
    [self.view addSubview:self.tableView];
    
    self.tableView.nothingRefreshView = [[UIRefreshNotingRefreshView alloc] initWithView:self.tableView viewController:self tryAgain:@selector(refreshData)];
    
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP], [self.ubo fw:UB_WIDTH oh:0.1f])];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP], [self.ubo fw:UB_WIDTH oh:0.1f])];
    
    
    self.emptyView = [[HZOrderGoodsEmptyView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP], [self.ubo fw:UB_WIDTH fh:UB_HEIGHT])];
    [self.emptyView hide];
    [self.view addSubview:self.emptyView];
    

    self.toolPanel = [[HZGoodsDetailToolPanel alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_BOTTOM uy:-kGoodsDetailToolPanelMainViewHeight],
                                                                            [self.ubo fw:UB_WIDTH uh:kGoodsDetailToolPanelMainViewHeight])];
    
    self.toolPanel.targetCallerBuyNow = [UBTargetCaller of:self action:@selector(buyNowBtnOnClick:)];
    self.toolPanel.targetCallerInsertCart = [UBTargetCaller of:self action:@selector(insertCartBtnOnClick:)];
    self.toolPanel.targetCallerGotoCart = [UBTargetCaller of:self action:@selector(gotoCartBtnOnClick)];
    [self.view addSubview:self.toolPanel];
    [self.toolPanel setHidden:YES];
    
    //打开系统上banner
    //backBtn
    self.backBtn = [[UIButton alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP],
                                                            [self.ubo ow:40 oh:40])];
    [self.backBtn setImage:[UIImage imageNamed:kImageMallArrowBackReturnRed] forState:UIControlStateNormal];
    self.backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.backBtn];
    
    
    
    //网络请求结果集
    self.remoteResult = [HZRemoteResult target:self action:@selector(finishLoadGoodsData:)];
    self.addCartResult = [HZRemoteResult target:self action:@selector(finishAddToCart:)];
    self.buyNowResult = [HZRemoteResult target:self action:@selector(finishBuyNow:)];
    
    [self.tableView.nothingRefreshView showLoadingMsg:@"卖命加载中.." errorMsg:@"加载失败，点击重试"];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    

    self.navigationController.navigationBar.hidden = NO;
    //加载商品数据
    if(self.data.headModel.topModel == nil){
        [self refreshData];
    }
    
 
    
}



- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}


#pragma mark 转到购物车页面
- (void)gotoCartBtnOnClick{
    self.tabBarController.selectedIndex = 2;
    [self.navigationController popToRootViewControllerAnimated:YES];
}



#pragma mark UIAlertViewDelegate
#pragma mark - alterview 代理
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
        {
         //do nothing
        }
            break;
        case 1:
        {
            [self gotoLoginViewController];
        }
            break;
            
        default:
            break;
    }
}

//检查用户是否登录
- (BOOL)checkUserLongin{
    NSString *uid = [[NSUserDefaults standardUserDefaults] stringForKey:@"uid"];
    if (uid && uid.length>0) {
        return YES;
    }
    return NO;
}

//跳转到登陆接口
- (void)gotoLoginViewController{
    self.navigationController.toolbarHidden = YES;
    
    
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"visitor"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    UIViewController * loginOut = [[UIStoryboard storyboardWithName:@"LogInOutStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"login"];
    loginOut.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:loginOut animated:YES];
}


#pragma  mark 点击立即购买
- (void)buyNowBtnOnClick:(NSString *)goodsNumber{
    
    if ([SNTool shouldLoginFromController:self]) {
        return;
    }
    
    NSString * myAreaID = [[NSUserDefaults standardUserDefaults] objectForKey:kMyAreaID];
    if (!myAreaID || !myAreaID.length || [myAreaID isEqualToString:@"0"]) {
        [MyToast showDefaultBottonWithText:@"亲,您还没绑定小区"];
        return;
    }
    
    
    NSString *selectedGiftID = @"";
    for (HZGoodsCuXiaoCellViewModel *model in self.data.headModel.promotionModel.topCellArray) {
        if (model.isSelected) {
            selectedGiftID = model.productID;
            break;
        }
    }
    
    HZMallHttpManager *httpManager = [HZMallHttpManager shareHttpRequestMananger];
    
    NSDictionary *params = @{APIKEY_mall_shop_id:self.data.headModel.topModel.shopID,
                             APIKEY_mall_product_id:self.data.headModel.topModel.productID,
                             APIKEY_mall_num:goodsNumber,
                             APIKEY_mall_gifts:selectedGiftID};
    [httpManager addProductToCartWithParams:params result:self.buyNowResult needBuyNow:YES];
}

#pragma mark 点击加入购物车
- (void)insertCartBtnOnClick:(NSString *)goodsNumber{
    
    if ([SNTool shouldLoginFromController:self]) {
        return;
    }
    
    NSString * myAreaID = [[NSUserDefaults standardUserDefaults] objectForKey:kMyAreaID];
    if (!myAreaID || !myAreaID.length || [myAreaID isEqualToString:@"0"]) {
        [MyToast showDefaultBottonWithText:@"亲,您还没绑定小区"];
        return;
    }
    
    NSString *selectedGiftID = @"";
    for (HZGoodsCuXiaoCellViewModel *model in self.data.headModel.promotionModel.topCellArray) {
        if (model.isSelected) {
            selectedGiftID = model.productID;
            break;
        }
    }
    
    /** 如果用户未登录，则先把商品加入到本地数据库 -write by khzliu */
    if (![self checkUserLongin]) {
        HZMallSqlDataManager *sqlManager = [HZMallSqlDataManager defaultManager];
        [sqlManager insertOfflineCartsCacheDataWithModel:self.data];
        [MyToast showDefaultBottonWithText:@"成功加入购物车"];
        
        /** 跟新下banner 购物车内物品的数量 -wirte by khzliu */
        self.data.cart = [NSString stringWithFormat:@"%d" ,[self.data.cart integerValue]+self.data.headModel.topModel.buyNum];
        
        //刷新购物车
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationCartsRefresh object:nil];
        
        //记录本地购物车内商品的数量
        [[NSUserDefaults standardUserDefaults] setObject:self.data.cart forKey:kHZUserProductNumInCarts];
        //发送通知 告知购物车数量已经改变
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotifacationUserProductNumInCartsChanged object:nil];

    }else{
        /** 用户已经登录 则把数据加入到网络中 -write by khzliu */
        HZMallHttpManager *httpManager = [HZMallHttpManager shareHttpRequestMananger];
        
        NSDictionary *params = @{APIKEY_mall_shop_id:self.data.headModel.topModel.shopID?self.data.headModel.topModel.shopID:@"0",
                                 APIKEY_mall_product_id:self.data.headModel.topModel.productID?self.data.headModel.topModel.productID:@"0",
                                 APIKEY_mall_num:goodsNumber?goodsNumber:@"1",
                                 APIKEY_mall_gifts:selectedGiftID};
        [httpManager addProductToCartWithParams:params result:self.addCartResult needBuyNow:NO];
    }
    
}

#pragma mark 返回
- (void)backBtnClick{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 点击客服
- (void)serviceBtnOnClick{
    if (self.data) {
        HDChatOBO *chatVC = [[HDChatOBO alloc] init];
        chatVC.chat_people_uid = self.data.userID;
        chatVC.chat_people_name = self.data.nickname;
        chatVC.chat_people_face = self.data.faceURL;
        chatVC.shop_id = self.data.headModel.topModel.productID;
        chatVC.shop_price = self.data.headModel.topModel.salePrice;
        chatVC.shop_image = self.data.headModel.topModel.productPic;
        chatVC.shop_name = self.data.headModel.topModel.productName;
        chatVC.isShop = @"1";
        
        [self.navigationController pushViewController:chatVC animated:YES];
    }
}


#pragma mark finish load product's details
- (void)finishLoadGoodsData:(HZRemoteResult *)result{
    [self.tableView endRefreshingAll];
    //隐藏全屏菊花
    [MBProgressHUD dismissMBProgressHUBOnWindows];
    
    if (result.response.status == JKResponseServerError || result.response.status == JKResponseLocalError) {
        [self.tableView.nothingRefreshView activeStop];
        [self.emptyView hide];
        [self.toolPanel setHidden:YES];
        [MyToast showDefaultBottonWithText:@"加载失败，服务器或本地网络错误"];
    }else if (result.response.status == JKResponseOK) {
        [self.toolPanel setHidden:NO];
        
        
        self.data = result.info;
        
        [self.emptyView hide];
        
        if (!self.data.headModel.topModel) {
            [self.tableView.nothingRefreshView remove];
            if(result.response.status == JKResponseTail){
                [self.emptyView showWithMessage:@"抱歉，暂无此商品!"];
            }else{
                [self.emptyView showWithMessage:result.response.message];
            }
        }else{
            [self.picHeadView fillViewWithModel:self.data.headModel.topModel];
            [self.totalCmtHeaderView showPinglunNum:self.data.headModel.topModel.pinglunNum];            
            
            [self.tableView reloadData];
            
            [self.tableView.nothingRefreshView remove];
        }
        
        
    }else{
        [self.tableView.nothingRefreshView remove];
        if(result.response.status == JKResponseTail){
            [self.emptyView showWithMessage:@"抱歉，暂无此商品!"];
        }else{
            [self.emptyView showWithMessage:result.response.message];
        }
        
    }

}




#pragma mark finish add to cart
- (void)finishAddToCart:(HZRemoteResult *)result{
    if (result.response.OK) {
        [MyToast showDefaultBottonWithText:@"成功加入购物车"];
        
        /** 跟新下banner 购物车内物品的数量 -wirte by khzliu */
        self.data.cart = [NSString stringWithFormat:@"%d" ,[self.data.cart integerValue]+self.data.headModel.topModel.buyNum];
        
        //刷新购物车
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationCartsRefresh object:nil];
        
        
        //记录本地购物车内商品的数量
        [[NSUserDefaults standardUserDefaults] setObject:self.data.cart forKey:kHZUserProductNumInCarts];
        //发送通知 告知购物车数量已经改变
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotifacationUserProductNumInCartsChanged object:nil];


        
    }else if(result.response.status == JKResponseGoodsOutOfStock){
        [MyToast showDefaultBottonWithText:@"商品缺货"];
    }else if (result.response.status == JKResponseStatusGoodsOffShelves){
        [MyToast showDefaultBottonWithText:@"商品已下架"];
    }else{
        [MyToast showDefaultBottonWithText:@"加入购物车失败"];
    }

}
#pragma mark finish buy now
- (void)finishBuyNow:(HZRemoteResult *)result{
    if (result.response.OK) {
        
        //存储已经选择的商品
        HZMallSqlDataManager *sqlManager = [HZMallSqlDataManager defaultManager];
        [sqlManager updateProductChoseStatuWithProductID:self.data.headModel.topModel.productID selected:YES];
        
        //刷新购物车
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationCartsRefresh object:nil];
        
        //切换到购物车
        self.tabBarController.selectedIndex = 3;
        
        [self.navigationController popToRootViewControllerAnimated:YES];
        
        
        
    }else if(result.response.status == JKResponseGoodsOutOfStock){
        [MyToast showDefaultBottonWithText:@"商品缺货"];
    }else if (result.response.status == JKResponseStatusGoodsOffShelves){
        [MyToast showDefaultBottonWithText:@"商品已下架"];
    }else{
        [MyToast showDefaultBottonWithText:@"购买失败"];
    }
}


#pragma mark 下拉刷新
- (void)refreshData{

    if (self.data.headModel.topModel == nil) {
        [self.tableView.nothingRefreshView showLoadingMsg:@"卖命加载中.." errorMsg:@"加载失败，点击重试"];
    }
    
    HZMallHttpManager *httpManager = [HZMallHttpManager shareHttpRequestMananger];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (self.shopID.length) {
        [params setObject:self.shopID forKey:APIKEY_mall_shop_id];
    }
    
    if (self.productID.length) {
        [params setObject:self.productID forKey:APIKEY_mall_product_id];
    }
    
    if (params.count) {
        [httpManager loadGoodsDetailsWithParams:params result:self.remoteResult];
    }else{
        [self.emptyView showWithMessage:@"抱歉，没有找到符合条件的商品!"];
    }

}


#pragma mark 点击浏览商品大图
- (void)seeProductsBigPictures:(NSDictionary *)dict{
    
    MJPhotoBrowser *photoBrowser = [[MJPhotoBrowser alloc] init];
    NSMutableArray *photoArray = [NSMutableArray array];
    NSArray *pics = [dict arrayForKey:@"array"];
    NSString *picURL = [dict stringForKey:@"urlKey" nilValue:@""];
    NSInteger index = [dict integerForKey:@"index"];
    for (NSDictionary *pic in pics) {
        MJPhoto *photo = [[MJPhoto alloc] init];
        //展位图
        UIImageView *photoPlaceHolder = [[UIImageView alloc] init];
        [photoPlaceHolder setImageWithURL:[pic stringForKey:picURL nilValue:@""]];
        photo.srcImageView = photoPlaceHolder;
        //原图
        photo.url = [NSURL URLWithString:[[pic stringForKey:picURL nilValue:@""] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        photo.save = NO;
        [photoArray addObject:photo];
    }
    photoBrowser.photos = [NSArray arrayWithArray:photoArray];
    
    if (index>=pics.count) {
        index = 0;
    }
    photoBrowser.currentPhotoIndex = index;
    [photoBrowser show];

}





#pragma -mark 查看更多须知
- (void)seeViewNotice{
    
    self.navigationController.navigationBar.hidden = NO;
    HZNormalWebViewController *salesVC = [[HZNormalWebViewController alloc] init];
    salesVC.webTitle = @"购买须知";
    salesVC.webURL = self.data.headModel.topModel.deliveryURL;
    [self.navigationController pushViewController:salesVC animated:YES];
}

#pragma mark 查看商品全部评论
- (void)seeAllComments{
    self.navigationController.navigationBar.hidden = NO;
    HZProuctCommentsViewController *allCmsVC = [[HZProuctCommentsViewController alloc] init];
    allCmsVC.productID = self.data.headModel.topModel.productID;
    [self.navigationController pushViewController:allCmsVC animated:YES];
}




#pragma mark 点击查看评论大图
- (void)seeCommentsPics:(NSDictionary *)dict{
    MJPhotoBrowser *photoBrowser = [[MJPhotoBrowser alloc] init];
    NSMutableArray *photoArray = [NSMutableArray array];
    NSArray *pics = [dict arrayForKey:@"array"];
    NSString *picURL = [dict stringForKey:@"urlKey" nilValue:@""];
    NSInteger index = [dict integerForKey:@"index"];
    for (NSDictionary *pic in pics) {
        MJPhoto *photo = [[MJPhoto alloc] init];
        //展位图
        UIImageView *photoPlaceHolder = [[UIImageView alloc] init];
        [photoPlaceHolder setImageWithURL:[pic stringForKey:picURL nilValue:@""]];
        photo.srcImageView = photoPlaceHolder;
        //原图
        photo.url = [NSURL URLWithString:[[pic stringForKey:picURL nilValue:@""] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        photo.save = NO;
        [photoArray addObject:photo];
    }
    photoBrowser.photos = [NSArray arrayWithArray:photoArray];
    
    if (index>=pics.count) {
        index = 0;
    }
    photoBrowser.currentPhotoIndex = index;
    [photoBrowser show];
}

#pragma mark 点击查看详情大图
- (void)seeDetailsPic:(HZProductTextImageDetailModel *)model{
    
    if (!model || model.pic.length<=0) {
        return;
    }
    
    MJPhotoBrowser *photoBrowser = [[MJPhotoBrowser alloc] init];
    NSMutableArray *photoArray = [NSMutableArray array];


    MJPhoto *photo = [[MJPhoto alloc] init];
    //展位图
    UIImageView *photoPlaceHolder = [[UIImageView alloc] init];
    [photoPlaceHolder setImageWithURL:model.pic];
    photo.srcImageView = photoPlaceHolder;
    //原图
    photo.url = [NSURL URLWithString:[model.pic stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    photo.save = NO;
    [photoArray addObject:photo];

    photoBrowser.photos = [NSArray arrayWithArray:photoArray];
    
    photoBrowser.currentPhotoIndex = 0;
    [photoBrowser show];
}

#pragma mark UITableViewDelegate UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (self.data.headModel.topModel == nil) {
        return [[UIView alloc] init];
    }
    
    if (section == 0) {
        
        return self.picHeadView;
    }else if(section == 1){
        return self.detailSecHeaderView;
        
    }else if(section == 2){
        return self.pslcSecHeaderView;
    }else{
        return self.totalCmtHeaderView;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (self.data.headModel.topModel == nil) {
        return 0.01f;
    }
    if (section == 0) {
       return self.data.headModel.topModel.viewHeight;
    }else if(section == 1){
        return kHZGoodsDetailHeadViewHeight;
    }else if(section == 2){
        return [self.view.ubo uh:kHZProductTextImageDetailFootViewHeight];
    }else{
        return [self.view.ubo uh:kHZGoodsDetailTotalCmtViewHeight];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (self.data.headModel.topModel == nil) {
        return 0.01f;
    }
    
    if (section >=3 ) {
        return 0.01f;
    }
    return 20.0f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSInteger cellNumber = 0;
    if (section == 0) {
        return cellNumber;
    }else if (section == 1) {
 
       cellNumber = [self.data.headModel.topModel.extendIntors count];
        return cellNumber;
    }else if(section == 2){
        
        return 0;
    }else if (section == 3){
        if(self.data.pinlunArray){
            cellNumber = [self.data.pinlunArray count];
        }else{
            cellNumber = 0;
        }
    }
    return cellNumber;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 1) {
        
        if (self.data.headModel.topModel.extendIntors.count <= 0) {
            UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"UITableViewCellNothing"];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCellNothing"];
                cell.textLabel.font = JKAppGlobleFontMinTagText;
                cell.textLabel.textAlignment = NSTextAlignmentCenter;
                cell.textLabel.textColor = HZ_MALL_TEXT_GRAY_COLOR;
            }
            cell.textLabel.text = @"无图文详情";
            return cell;
        }
        
        HZProductTextImageDetailCell *cell = [HZProductTextImageDetailCell cellWithTableView:(HZTableView *)tableView];
        cell.model = [self.data.headModel.topModel.extendIntors objectAtIndex:indexPath.row];
        cell.picsTargetCaller = [UBTargetCaller of:self action:@selector(seeDetailsPic:)];
        return cell;
        
        
    }else if(indexPath.section == 2){
        
        if (self.data.headModel.topModel.specifications.count <= 0) {
            UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"UITableViewCellNothing"];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCellNothing"];
                cell.textLabel.font = JKAppGlobleFontMinTagText;
                cell.textLabel.textAlignment = NSTextAlignmentCenter;
                cell.textLabel.textColor = HZ_MALL_TEXT_GRAY_COLOR;
            }
            cell.textLabel.text = @"无规格参数";
            return cell;
        }
        
        HZGoodsParametersCell *cell = [HZGoodsParametersCell cellWithTableView:(HZTableView *)tableView];
        [cell showWithDictionry:[self.data.headModel.topModel.specifications objectAtIndex:indexPath.row] nameKey:APIKEY_mall_name valueKey:APIKEY_mall_val];
        return cell;
        
    }else if (indexPath.section == 3){
        
        if(!self.data || self.data.pinlunArray.count<= 0){

            UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"UITableViewCellNothing"];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCellNothing"];
                cell.textLabel.font = JKAppGlobleFontMinTagText;
                cell.textLabel.textAlignment = NSTextAlignmentCenter;
                cell.textLabel.textColor = HZ_MALL_TEXT_GRAY_COLOR;
            }
            cell.textLabel.text = @"";
            return cell;

        }
        
        
        
        HZGoodsCommentCell *cell = [HZGoodsCommentCell cellWithTableView:(HZTableView *)tableView];
        cell.picsTargetCaller = [UBTargetCaller of:self action:@selector(seeCommentsPics:)];
        cell.model = [self.data.pinlunArray objectAtIndex:indexPath.row];
        return cell;
    }
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    cell.textLabel.text = @"无内容";
    return cell;
    
    

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 0;
    }else if (indexPath.section == 1) {
        
        if (self.data.headModel.topModel.extendIntors.count<= 0) {
            return 44;
        }
        
        HZProductTextImageDetailModel *model = [self.data.headModel.topModel.extendIntors objectAtIndex:indexPath.row];
        return model.cellHeight;
        
    }else if(indexPath.section == 2){
        if (self.data.headModel.topModel.specifications.count<= 0) {
            return 44;
        }
        return floor([[self.data.headModel.topModel.specificationsCellHeightArray objectAtIndex:indexPath.row] floatValue]);
        
    }else if (indexPath.section == 3){
        if (self.data.pinlunArray.count<= 0) {
            return 44;
        }
        
        HZGoodsCommentModel *model = [self.data.pinlunArray objectAtIndex:indexPath.row];
        return model.cellHeight;
    }
    return 44.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    
//    self.navigationController.navigationBar.hidden = NO;
//    HZProductTextImageDetailViewController *prodTxtImgVC = [[HZProductTextImageDetailViewController alloc] init];
//    prodTxtImgVC.toolPanel = self.toolPanel;
//    prodTxtImgVC.deliveryURL = self.data.headModel.deliveryURL;
//    prodTxtImgVC.deliveryDataArray = self.data.headModel.topModel.extendIntors;
//    prodTxtImgVC.productID = self.data.headModel.topModel.productID;
//    prodTxtImgVC.arvsDataArray = self.data.headModel.topModel.specifications;
//    [prodTxtImgVC showMoreDetailsWithDefaultTag:kGoodsMoreDetailBarTagComment];
//    [self.navigationController pushViewController:prodTxtImgVC animated:YES];
    
}




@end
